/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.io.*;

/**
 * <p>Project: app2022 - ObjectStream1
 * <p>Powered by webrx On 2022-01-19 10:22:29
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class ObjectStream1 {
    public static void main(String[] args) {
        //保存对象
        //write();


        //读取对象
        try (var ois = new ObjectInputStream(new FileInputStream("bank.dat"))) {
            while (true) {
                var a = ois.readObject();
                System.out.println(a);
            }
        } catch (FileNotFoundException e) {
            System.out.println("没有bank.dat文件");
        } catch (IOException e) {
            System.out.println("数据读取完成");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * Description: List<User>
     *
     * @date: 2022/1/19 10:44
     * @params: []
     * @return: void
     */
    public static void write() {
        try (var oo = new ObjectOutputStream(new FileOutputStream("bank.dat", true))) {
            var u = new User(100, "李四", "河南郑州");
            System.out.println(u);
            oo.writeObject(u);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
